package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ReportServiceImlp implements ReportService {

@Autowired
private OrderMapper orderMapper;
@Autowired
private UserMapper userMapper;

    //统计日期区间内每一天的销售额
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        //获取每一天的日期并用“，”隔开
        List<LocalDate> dateList = new ArrayList<>();

        while (!begin.equals(end))
        {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //获取每天的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime =LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate,LocalTime.MAX);
            //查询每天的销售额，状态为已完成的订单
            Map map =new HashMap();
            map.put("begin" , beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double sum = orderMapper.selectBymap(map);
            turnoverList.add(sum == null ? 0.0 : sum);
        }

        //封装数据返回


        return TurnoverReportVO.builder()
                .dateList(StringUtil.join(String.valueOf(dateList),","))
                .turnoverList(StringUtil.join(String.valueOf(turnoverList),","))
                .build();

    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {

        //计算日期的每一天
        //获取每一天的日期并用“，”隔开
        List<LocalDate> dateList = new ArrayList<>();

        while (!begin.equals(end))
        {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //统计用户总量
        //查询用户表
        //统计新增用户
        List<Double> newuserList = new ArrayList<>();
        List<Double> userlist =new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate,LocalTime.MAX);
            Double newusersum = userMapper.selectByBeginanden(beginTime,endTime);
            newuserList.add(newusersum);
            Double usersum = userMapper.selectByBeginandend(endTime);
            userlist.add(usersum);
        }

        return UserReportVO.builder()
                .dateList(StringUtil.join(String.valueOf(dateList),","))
                .totalUserList(StringUtil.join(",", userlist))
                .newUserList(StringUtil.join(String.valueOf(newuserList),","))
                .build();
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        //计算日期的每一天
        //获取每一天的日期并用“，”隔开
        List<LocalDate> dateList = new ArrayList<>();
        while (!begin.equals(end))
        {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

         List<Integer> orderCountlist = new ArrayList<>();
        List<Integer> validOrdeCountList = new ArrayList<>();
        //查询时间区间内每日订单总数
        Integer ordercount = 0;
        Integer validOrdercount=0;
        //查询时间区间内每日有效订单数量
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate,LocalTime.MAX);
            Integer orderCount = orderMapper.getOrderCount(beginTime,endTime ,null);
            ordercount+=orderCount;
            Integer validOrderCount = orderMapper.getOrderCount(beginTime,endTime,Orders.COMPLETED);
            validOrdercount+=validOrderCount;
            orderCountlist.add(orderCount);
            validOrdeCountList.add(validOrderCount);
        }
        //计算订单完成率
        Double OrderLV = (double) (validOrdercount/ordercount);
        //封装数据返回
        return OrderReportVO.builder()
                .dateList(StringUtil.join(String.valueOf(dateList),","))
                .orderCountList(StringUtil.join(String.valueOf(orderCountlist),","))
                .validOrderCountList(StringUtil.join(String.valueOf(validOrdeCountList),","))
                .orderCompletionRate(OrderLV)
                .totalOrderCount(ordercount)
                .validOrderCount(validOrdercount)
                .build();
    }

    @Override
    public SalesTop10ReportVO top(LocalDate begin, LocalDate end) {

        LocalDateTime beginTime = LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end,LocalTime.MAX);

        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime,endTime);
        List<String> nameList = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        for (GoodsSalesDTO goodsSalesDTO : salesTop10) {
            nameList.add(goodsSalesDTO.getName());
            numberList.add(goodsSalesDTO.getNumber());
        }
        return SalesTop10ReportVO.builder()
                .nameList(StringUtil.join(String.valueOf(nameList),","))
                .numberList(StringUtil.join(String.valueOf(numberList),","))
                .build();
    }


}
